<template>
	<view>
		<u-sticky><u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs></u-sticky>
		<view class="content wrap" v-if="current == 0">
			<u-form :model="form2" ref="uForm"  :rules="rules">
				<u-form-item label="调出仓库*" label-width="180" left-icon="home"><u-input v-model="form2.warehouseNameOut" type="select" @click="getow()"/></u-form-item>
				
				<u-form-item label="调入仓库*" label-width="180" left-icon="home"><u-input v-model="form2.warehouseNamePut" type="select" @click="getpw()" placeholder="请选择调入仓库"/></u-form-item>
				
				<u-form-item label="商品*" label-width="150">
					<u-radio-group v-model="form2.goodsName">
						<u-radio 
							@change="radioChange2" 
							v-for="(item, index) in glist" :key="index" 
							:name="item.name"
						>
						{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				
				<u-form-item label="重量*" label-width="180" prop="nweight"><u-input v-model="form2.nweight" type="digit" placeholder="请输入重量"/></u-form-item>
			</u-form>
			<u-button @click="submit2">提交</u-button>
			
			<view>
				<u-select v-model="show" :list="owlist" @confirm="confirm"></u-select>
			</view>
			<view>
				<u-select v-model="show2" :list="pwlist" @confirm="confirm2"></u-select>
			</view>
			<u-card padding="16">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="12" text-align="center">
								<view class="weikaishi3">
									<u-icon name="search" size="30" labelSize="15px" labelColor="#000000" label="库存信息"></u-icon>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<u-row>
							<u-col span="3" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>仓库</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>商品</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>重量</span>
								</view>
							</u-col>
							<u-col span="5" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>日期</span>
								</view>
							</u-col>
						</u-row>
						<u-row v-for="(item,index) in dataList" :key="index">
							<u-col span="3" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.warehouseName}}{{item.positionNumber}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.goodsName}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.weight}}</span>
								</view>
							</u-col>
							<u-col span="5" text-align="center">
								<view class=" u-m-t-10 weikaishi6 huanghang jinxingzhong">
									<span>{{item.producetime}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</u-card>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
		<view class="" v-if="current == 1">
			<u-card padding="16" v-for="(item,index) in dataList2" :key="index">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="8" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi">
									<span>时间：{{item.times}}</span>
								</view>
							</u-col>
							<u-col span="4" text-align="right">
								<view class="u-line-1 u-m-t-2 weikaishi">
									<span>商品：{{item.goodsName}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<u-row>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>调出仓库：</span><span>{{item.warehouseNameOut}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>调入仓库：</span><span>{{item.warehouseNamePut}}</span>
								</view>
							</u-col>
							<u-col span="4" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>净重：</span><span>{{item.nweight}}</span>
								</view>
							</u-col>
							<u-col span="8" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>生产日期：</span><span>{{item.producetime}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>操作人：</span><span>{{item.userName}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</u-card>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				flex:'',
				list: [
					{
						name: '添加调拨'
					}, 
					{
						name: '调拨查询'
					}
				],
				current: 0,
				dataList:[],
				dataList2: [],
			    pageNo: 1,
			    hasMore: true,
			    orderCount: 0,
				form2:{
				    warehouseNameOut:'加工仓',
				    warehouseNamePut:"",
					goodsName:'',
					nweight:'',
				},
				glist:[],
				owlist:[
					{
						value:"加工仓",
						label:"加工仓"
					},
					{
						value:"直发仓",
						label:"直发仓"
					},
					{
						value:"次果仓",
						label:"次果仓"
					},
					{
						value:"废果仓",
						label:"废果仓"
					},
				],
				pwlist:[
					{
						value:"加工仓",
						label:"加工仓"
					},
					{
						value:"直发仓",
						label:"直发仓"
					},
					{
						value:"次果仓",
						label:"次果仓"
					},
					{
						value:"废果仓",
						label:"废果仓"
					},
				],
				show:false,
				show2:false,
			    rules: {
					nweight: [
						{ 
							required: true,
							message: '请输入重量', 
							trigger: 'blur',
						}
					],
				}
			}
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if(this.current == 1){
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++ 
					setTimeout(() => {
						this.loadOrder({
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			this.refresh()
			
			this.$http.post('/rest/fruits/findgoodslist',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				if(res.data.data.length>0){
					this.form2.goodsName = res.data.data[0].goodsName
					for(let item1 of res.data.data) {
						let obj = {}
						obj.name = item1.goodsName
						this.glist.push(obj)
					}
				}
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			sectionChange(e){
				this.current = e
				this.refresh()
			},
			getow(){
				this.show = true
			},
			getpw(){
				this.show2 = true
			},
			confirm(e) {
				this.form2.warehouseNameOut = e[0].label
			},
			confirm2(e) {
				this.form2.warehouseNamePut = e[0].label
			},
			submit2() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof this.form2.warehouseNamePut == 'undefined' || this.form2.warehouseNamePut == null || this.form2.warehouseNamePut == ""){
							uni.showToast({
								title: "请选择调入仓库",
								icon: 'none'
							})
							return
						}
						if(typeof this.form2.goodsName == 'undefined' || this.form2.goodsName == null || this.form2.goodsName == ""){
							uni.showToast({
								title: "请选择商品",
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/fruits/addAllocation',{
						},{
							params: this.form2,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								this.form2.nweight = ''
								this.$refs.uToast.show({
									title: '操作成功',
									type: 'success'
								})
								this.refresh()
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
			refresh(){
				uni.showLoading({
					title: '加载中...'
				})
				if(this.current == 0){
					this.$http.post('/rest/fruits/findStorageStockSumBySpec4',{
					},{
						params: {
						},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						this.dataList = res.data.data
					})
				} else if(this.current == 1){
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
					}).then(res => {
					})
				}
				uni.hideLoading()
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 10
					})
					this.$http.post('/rest/fruits/findAllocationPage',{
					},{
						params: param,
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.dataList2 = res.data.data
						} else {
							this.dataList2 = this.dataList2.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.hasMore = this.pageNo * 10 < res.data.count
						resolve(res)
					})
				})
			}
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	
	.wrap {
		padding: 30rpx;
	}
	.weikaishi2{
		font-size: 36rpx;
	}
	.weikaishi3{
		font-size: 32rpx;
	}
	.huanhang{
		word-break:break-all;
	}
</style>
